/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package main.environment.loader;

import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import main.common.Tile;

/**
 *
 * @author Fox
 */
public class EnvironmentMappings {
    
    private static final String mappingFile = "./media/environment/tiles.config";
    
    private HashMap map = new HashMap();
    
    public EnvironmentMappings() {
        try {
            loadMappings();
        } catch (Exception ex) {
            Logger.getLogger(EnvironmentMappings.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void loadMappings() throws Exception {
        BufferedReader br;
        br = new BufferedReader(new FileReader(mappingFile));
        importMappings(br);
        br.close();
    }
    
    private void importMappings(BufferedReader br) {
        String s;
        try {
            while ((s = br.readLine()) != null) {
                if (!s.startsWith("//")) {
                    String split[] = s.split(",");

                    Integer colourByte = Integer.parseInt(split[0]);

                    if (!split[1].equals("empty")) {
                        if (split.length<4) {
                                map.put(
                                    colourByte,
                                    new Tile(split[1])
                                    );
                        } else {
                            RescaleOp rescaleOp;
                            if (split.length == 5) {
                                rescaleOp = new RescaleOp(Float.parseFloat(split[3]), Float.parseFloat(split[4]), null);
                            } else {
                                float scales[] = {Float.parseFloat(split[3]),Float.parseFloat(split[4]),Float.parseFloat(split[5]),Float.parseFloat(split[6])};
                                float offsets[] = {Float.parseFloat(split[7]),Float.parseFloat(split[8]),Float.parseFloat(split[9]),Float.parseFloat(split[10])};
                                
                                rescaleOp = new RescaleOp(scales, offsets, null);
                            }
                            
                            BufferedImage img = ImageIO.read(new File(split[1]));

                            rescaleOp.filter(img, img);
                            
                            map.put(
                                colourByte,
                                new Tile(split[1])
                                );    
                        }
                    } else {
                        map.put(
                            colourByte,
                            null);
                    }

                    //file for Wall Object
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(EnvironmentMappings.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Tile mapGet(int i) throws Exception{
        
        if (i==-1) return null;
        
        Object s = map.get(i);
        
        if (s == null) {
            System.err.println(i + " not mapped. Reloading tiles.config");
            loadMappings();
            return null;
        } else {
            return (Tile) s;
        }
    }
}
